package com.whcoding.swagger.controller;

import com.whcoding.swagger.common.ApiResponse;
import com.whcoding.swagger.common.DataType;
import com.whcoding.swagger.common.ParamType;
import com.whcoding.swagger.pojo.OrmCustomerVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @program: spring-boot-learning
 * @description:
 * @author: whcoding
 * @create: 2022-02-17 15:38
 **/
@RestController
@RequestMapping("/customer")
@Api(tags = "V1.0 客户管理 ", description = "客户管理", value = "客户管理")
public class CustomerControler {

    private static final Logger log = LoggerFactory.getLogger(CustomerControler.class);

    @GetMapping
    @ApiOperation(value = "条件查询", notes = "备注")
    @ApiImplicitParams({@ApiImplicitParam(name = "customerName", value = "客户名称", dataType = DataType.STRING, paramType = ParamType.QUERY, defaultValue = "xxx")})
    public ApiResponse<OrmCustomerVO> getByCustomerName(String customerName) {
        log.info("多个参数用  @ApiImplicitParams");
        return ApiResponse.<OrmCustomerVO>builder().code(200).message("操作成功").data(getCustomerVO()).build();
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "主键查询", notes = "备注")
    @ApiImplicitParams({@ApiImplicitParam(
            name = "id",
            value = "客户编号",
            dataType = DataType.INT,
            paramType = ParamType.PATH,
            defaultValue = "0",
            example = "0"
    )})
    public ApiResponse<OrmCustomerVO> getByCustomerId(@PathVariable Integer id) {
        log.info("单个参数用  @ApiImplicitParam");
        return ApiResponse.<OrmCustomerVO>builder().code(200).message("操作成功").data(getCustomerVO()).build();
    }

    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除客户", notes = "备注")
    @ApiImplicitParam(name = "id", value = "客户编号", dataType = DataType.INT, paramType = ParamType.PATH, defaultValue = "0", example = "0")
    public void delete(@PathVariable Integer id) {
        log.info("单个参数用 ApiImplicitParam");
    }

    @PostMapping
    @ApiOperation(value = "添加客户")
    public OrmCustomerVO post(@RequestBody OrmCustomerVO user) {
        log.info("如果是 POST PUT 这种带 @RequestBody 的可以不用写 @ApiImplicitParam");
        return user;
    }

    @PostMapping("/multipar")
    @ApiOperation(value = "添加客户")
    public List<OrmCustomerVO> multipar(@RequestBody List<OrmCustomerVO> user) {
        log.info("如果是 POST PUT 这种带 @RequestBody 的可以不用写 @ApiImplicitParam");

        return user;
    }

    @PostMapping("/array")
    @ApiOperation(value = "添加客户")
    public OrmCustomerVO[] array(@RequestBody OrmCustomerVO[] user) {
        log.info("如果是 POST PUT 这种带 @RequestBody 的可以不用写 @ApiImplicitParam");
        return user;
    }

    @PutMapping("/{id}")
    @ApiOperation(value = "修改客户")
    public void put(@PathVariable Long id, @RequestBody OrmCustomerVO user) {
        log.info("如果你不想写 @ApiImplicitParam 那么 swagger 也会使用默认的参数名作为描述信息 ");
    }

    @PostMapping("/{id}/file")
    @ApiOperation(value = "文件上传")
    public String file(@PathVariable Long id, @RequestParam("file") MultipartFile file) {
        log.info(file.getContentType());
        log.info(file.getName());
        log.info(file.getOriginalFilename());
        return file.getOriginalFilename();
    }

    /**
     * 获取list数据
     *
     * @return
     */
    public List<OrmCustomerVO> getCustomerVOList() {
        List<OrmCustomerVO> customerVOList = new ArrayList<>();
        OrmCustomerVO c1 = new OrmCustomerVO();
        c1.setId(Long.parseLong("1"));
        c1.setCustomerName("西安客户");
        c1.setCustomerNo("XA001");
        c1.setCustomerId(001);
        c1.setCustomerAddress("西安市雁塔区鱼化寨街道天谷八路数字出版基地");
        c1.setCustomerType(1);
        c1.setCustomerFund(new BigDecimal("366.36"));
        c1.setOrderDate(new Date());
        c1.setOrderNo("2020081601");
        c1.setOrderId("ID_01");
        c1.setIsValid(1);


        OrmCustomerVO c2 = new OrmCustomerVO();
        c2.setId(Long.parseLong("1"));
        c2.setCustomerName("上海客户");
        c2.setCustomerNo("SH001");
        c2.setCustomerId(002);
        c2.setCustomerAddress("上海市青浦区重固镇北青公路6558号意邦大厦");
        c2.setCustomerType(2);
        c2.setCustomerFund(new BigDecimal("1366.36"));
        c2.setOrderDate(new Date());
        c2.setOrderNo("2020081602");
        c2.setOrderId("ID_02");
        c2.setIsValid(1);

        OrmCustomerVO c3 = new OrmCustomerVO();
        c3.setId(Long.parseLong("1"));
        c3.setCustomerName("杭州客户");
        c3.setCustomerNo("HZ001");
        c3.setCustomerId(003);
        c3.setCustomerAddress("杭州市");
        c3.setCustomerType(3);
        c3.setCustomerFund(new BigDecimal("166.36"));
        c3.setOrderDate(new Date());
        c3.setOrderNo("2020081603");
        c3.setOrderId("ID_03");
        c3.setIsValid(1);
        customerVOList.add(c1);
        customerVOList.add(c2);
        customerVOList.add(c3);
        return customerVOList;
    }

    /**
     * 获取单个数据
     *
     * @return
     */
    public OrmCustomerVO getCustomerVO() {
        return getCustomerVOList().get(0);
    }


}
